package com.tarena.lbs.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.mapper.UserTagsMapper;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.po.UserTagsPO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserTagsMapper userTagsMapper;
    @Override
    public Long PhoneConfirm(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Phone", phone);
        Long num = userMapper.selectCount(queryWrapper);
        // 执行查询并返回是否存在
        return  num;
    }

    @Override
    public void register(UserParam param) {
        if(param.getId()==null){
            UserPO  userPO = new UserPO();
            BeanUtils.copyProperties(param, userPO);
            userPO.setRegTime(new Date());
            userPO.setLastLogTime(new Date());
            userPO.setStatus(0);
            userPO.setUnionId(UUID.randomUUID().toString());
            log.debug("插入用户信息：{}", userPO);
            userMapper.insert(userPO);
        }else {
            UserPO userPO = new UserPO();
            BeanUtils.copyProperties(param, userPO);
            userPO.setLastLogTime(new Date());
            log.debug("更新用户信息：{}", userPO);
            userMapper.updateById(userPO);
        }

    }

    @Override
    public UserVO detail() {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        log.debug("用户信息：{}", userPrinciple);
        Integer id = userPrinciple.getId();
        UserPO userPO = userMapper.selectById(id);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userPO, userVO);
        return userVO;
    }

    @Override
    public void addTags(UserTagsParam param) {
        String[] ids = param.getTagIds().split(",");
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        for (String id : ids){
            UserTagsPO userTagsPO = new UserTagsPO();
            userTagsPO.setTagId(Integer.parseInt(id));
            userTagsPO.setUserId(userTagsPO.getUserId());
            userTagsMapper.insert(userTagsPO);
        }
        UserPO  userPO = new UserPO();
        userPO.setId(userPrinciple.getId());
        userPO.setStatus(1);
        log.debug("更新用户信息：{}", userPO);
        userMapper.updateById(userPO);
    }
}
